﻿

using DG.Tweening;
using QFramework;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace ProjectChildhood
{
    public class ToolHand : ITool, IController
    {
        public string Name => "手";

        public int Range => Global.HandUpgradeUnlock ? 2 : 1;

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }

        public bool Selectable(ToolData toolData)
        {
            return toolData.ShowGrid[toolData.CellPos.x, toolData.CellPos.y] != null &&
                   toolData.ShowGrid[toolData.CellPos.x, toolData.CellPos.y].HasPlant &&
                   toolData.ShowGrid[toolData.CellPos.x, toolData.CellPos.y].PlantState == EPlantState.Ripe;
        }

        public void Use(ToolData toolData)
        {
            Plant plant = Object.FindObjectOfType<GridController>().PlantDatas[toolData.CellPos.x, toolData.CellPos.y] as Plant;
            
            if (plant.State == EPlantState.Ripe)
            {
                if (plant is Plant)
                {
                    if (!this.SendCommand(new AddItemCountCommand(plant.Name, 1))) // 工具栏满
                    {
                        return;
                    }
                    GameObject.Destroy(plant.Self);
                }
            }

            ChallengeSystem.OnPlantHarvast.Trigger(plant); // 触发挑战相关的事件
            toolData.ShowGrid[toolData.CellPos.x, toolData.CellPos.y].HasPlant = false;
            toolData.ShowGrid[toolData.CellPos.x, toolData.CellPos.y] = null;
            Object.FindObjectOfType<GridController>().PlantDatas[plant.XCell, plant.YCell] = null;

            // 飞入动画
            UI_Toolbar uI_Toolbar = UI_Toolbar.Instance;
            Image collectionIconTemplete = uI_Toolbar.collection_icon_templete;
            Vector2 plantScreenPoint =  RectTransformUtility.WorldToScreenPoint(Camera.main, toolData.GridCenterPos);
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(uI_Toolbar.transform as RectTransform, plantScreenPoint
                , null, out Vector2 plantPosInToolbar))
            {
                collectionIconTemplete.InstantiateWithParent(uI_Toolbar.transform)
                .LocalPosition(plantPosInToolbar)
                .Show()
                .Self(self =>
                {
                    UISlot targetPos = uI_Toolbar.ToolbarSlots.FirstOrDefault(slot => slot.Data != null && slot.Data.ItemName == plant.Name);
                    self.sprite = ResController.Instance.LoadToolSprite(Config.ItemWithName[plant.Name].Icon);
                    self.transform.localScale = Vector3.one * Random.Range(0.2f, 1.0f);
                    self.transform.DOScale(Vector3.one, 0.5f);
                    DOTween.Sequence()
                        .Append(self.transform.DOMove(self.transform.position + new Vector3(Random.Range(-0.5f, 0.5f), 1, 0).normalized * 128.0f, 0.3f))
                        .SetEase(Ease.OutCubic)
                        .Append(self.transform.DOMove(targetPos.transform.position, 0.2f)
                            .SetEase(Ease.InCubic)
                            .OnComplete(() =>
                        {
                            self.DestroyGameObj();
                        }));
                });
            }
            

            //音效
            AudioController.Instance.SFX_Harvest.Play();
            CameraController.Shake(CameraController.EShakeType.Middle);
        }
    }
}
